﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;

namespace EditoneLib
{
    public abstract partial class BaseTrack : ITrack
    {
        protected Dictionary<TimeSpan, IClip> TrackDictionary = new Dictionary<TimeSpan, IClip>();

        public event NotifyCollectionChangedEventHandler CollectionChanged = (a, b) => { };

        public int Count
        {
            get { return TrackDictionary.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public ICollection<TimeSpan> Keys
        {
            get { return TrackDictionary.Keys; }
        }

        public ICollection<IClip> Values
        {
            get { return TrackDictionary.Values; }
        }

        public IClip this[TimeSpan key]
        {
            get { return TrackDictionary[key]; }
            set
            {
                IClip originalValue = TrackDictionary[key];
                TrackDictionary[key] = value;

                CollectionChanged(this, new NotifyCollectionChangedEventArgs(
                    NotifyCollectionChangedAction.Replace, value, originalValue
                    ));
            }
        }

        public void Add(KeyValuePair<TimeSpan, IClip> item)
        {
            TrackDictionary.Add(item.Key, item.Value);
            CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item.Value));
        }

        public void Add(TimeSpan key, IClip value)
        {
            Add(new KeyValuePair<TimeSpan, IClip>(key, value));
        }

        public void Clear()
        {
            TrackDictionary.Clear();
            CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public bool Contains(KeyValuePair<TimeSpan, IClip> item)
        {
            return TrackDictionary.Contains(item);
        }

        public bool ContainsKey(TimeSpan key)
        {
            return TrackDictionary.ContainsKey(key);
        }

        public void CopyTo(KeyValuePair<TimeSpan, IClip>[] array, int arrayIndex)
        {
            TrackDictionary.ToArray().CopyTo(array, arrayIndex);
        }

        public IEnumerator<KeyValuePair<TimeSpan, IClip>> GetEnumerator()
        {
            return TrackDictionary.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable) TrackDictionary).GetEnumerator();
        }

        public bool Remove(TimeSpan key)
        {
            IClip value = this[key];
            bool success = TrackDictionary.Remove(key);
            if (success)
            {
                CollectionChanged(this,
                    new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, value));
            }
            return success;
        }

        public bool Remove(KeyValuePair<TimeSpan, IClip> item)
        {
            return Remove(item.Key);
        }

        public bool TryGetValue(TimeSpan key, out IClip value)
        {
            return TrackDictionary.TryGetValue(key, out value);
        }

    }
}